﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Facade;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.SalesOneWeb.ProductLibrary
{
    public partial class ManageProducts : SOSecurityPage, IDataPage, IntTest
    {
        private int pageNo = 1;
        List<Product> products;
        FinanceFacade productFacade = new FinanceFacade();

        protected void Page_Load(object sender, EventArgs e)
        {
            //lblMsg.Text = "";
            if (!IsPostBack)
            {
                //pageNo = 1;
                //UCGrid1.SetText("1"); 

                txtPlanName.Text = "";
                FillDropDownProductCategory("Productcategory");
                UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                //lblMsg.Text = "";
            }

            if (!IsPostBack)
            {
                //lblMsg.Text = "";
                if (ddlPlanCategory.SelectedValue == "-1")
                {
                    ddlPlanCode.Enabled = false;
                    txtPlanName.Enabled = false;
                    this.UCGridButtons1.Visible = false;
                }
            }
            else if (IsPostBack)
            {
                if (ddlPlanCategory.SelectedValue == "-1")
                {
                    MassageSummery("IIS503");
                    UCGridButtons1.Visible = false;
                    Session["productCode"] = null;
                }
            }
            if (IsPostBack != null && ddlPlanCategory.SelectedValue != "-1")
            {
                this.UCGridButtons1.Visible = true;
                this.UCGrid1.Visible = true;
            }

            if (Session["successCodeUpdate"] != null)
            {
                MassageSummery("IIS402");
                Session.Remove("successCodeUpdate");
            }

            if (Session["failCodeUpdate"] != null)
            {
                MassageSummery("IIS502");
                Session.Remove("failCodeUpdate");
            }
        }

        #region IDataPage Members

        public void View()
        {

            if (Session["productCode"] == null)
            {

                Session["productCode"] = ((GridViewRow)Session["firstRow"]).Cells[4].Text;
                Session.Remove("firstRow");
            }

            if (Session["productCode"] != null)
            {
                Response.Redirect("~/ProductLibrary/ManageProducts.aspx?a=v", false);
            }
            else
            {
                MassageSummery("IIS502");
                Session["productCode"] = null;
            }
        }

        public void Update()
        {

            if (Session["productCode"] == null)
            {

                Session["productCode"] = ((GridViewRow)Session["firstRow"]).Cells[4].Text;
                Session.Remove("firstRow");
            }

            if (Session["productCode"] != null)
            {
                Response.Redirect("~/ProductLibrary/ManageProducts.aspx?a=u", false);
            }
            else
            {
                MassageSummery("IIS502");
                Session["productCode"] = null;
            }
        }

        public void Cancel()
        {

        }

        public void Delete()
        {

        }

        public void Create()
        {

        }

        #endregion

        public void FillDropDownProductCategory(string type)
        {
            //pageNo = 1;
            //UCGrid1.SetText("1"); 

            List<ProductCategory> pc = productFacade.GetProductCategories("BenefitName ASC");
            ProductCategory productCategory = new ProductCategory();
            productCategory.ProductCategoryCode = -1;
            productCategory.ProductCategoryName = "Select...";
            pc.Insert(0, productCategory);
            ddlPlanCategory.DataSource = pc;
            ddlPlanCategory.DataTextField = "ProductCategoryName";
            ddlPlanCategory.DataValueField = "ProductCategoryCode";
            ddlPlanCategory.DataBind();

        }

        public void FillDropDownProduct(int planCat)
        {
            //pageNo = 1;
            //UCGrid1.SetText("1"); 

            List<Product> p = productFacade.GetProductsss(planCat);
            Product product = new Product();
            product.PlanCode = "Select...";
            p.Insert(0, product);
            ddlPlanCode.DataSource = p;
            ddlPlanCode.DataTextField = "PlanCode";
            ddlPlanCode.DataValueField = "PlanCode";
            ddlPlanCode.DataBind();

        }

        public void Search(int planCat, int page)
        {
            //pageNo = 1;
            //UCGrid1.SetText("1"); 

            Product product = new Product();
            productFacade = new FinanceFacade();
            int[] hideColumn = { 1, 2, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 };

            String productCategory = ddlPlanCategory.SelectedValue;

            try
            {
                product.PlanName = txtPlanName.Text;
                product.ProductCategoryCode = Convert.ToInt32(ddlPlanCategory.SelectedValue);
                if (ddlPlanCode.SelectedValue == "Select...")
                {
                    product.PlanCode = "";
                }
                else
                {
                    product.PlanCode = ddlPlanCode.SelectedValue;
                }
                if (ddlPlanCategory.Text == "-1" && ddlPlanCode.Text == "" && txtPlanName.Text == "")
                {
                    MassageSummery("IIS503");
                }
                else
                {
                    products = productFacade.GetProducts(product, page);
                    if (products.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (productFacade.GetProducts(product, page + 1)).Count > 0 ? false : true;
                    }

                    if (products.Count == 0)
                    {
                        UCGridButtons1.Visible = false;
                        UCGrid1.Visible = false;
                        MassageSummery("IIS504");
                    }
                    else
                    {

                        UCGrid1.styleList = new List<GridViewStyle> {

                new GridViewStyle{
                    ColumnId = "Select",
                    ColumnHeader = "Select",
                    ColumnWidth = 100},
                new GridViewStyle{
                    ColumnId = "ProductCategoryName",
                    ColumnHeader = "Plan Category",
                    ColumnWidth = 180},
                new GridViewStyle{
                    ColumnId = "PlanCode",
                    ColumnHeader = "Plan Code",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "PlanName",
                    ColumnHeader = "Plan Name",
                    ColumnWidth = 300},
                new GridViewStyle{
                    ColumnId = "Status",
                    ColumnHeader = "Status",
                    ColumnWidth = 100}
                 
                    };

                        UCGrid1.SetDataSource(products, hideColumn);
                    }
                }
            }
            catch (Exception)
            {

            }

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pageNo = 1;
            Search((Convert.ToInt32(ddlPlanCategory.SelectedValue)), 1);
            UCGrid1.SetText("1");


        }

        #region IDataPage Members - btn session

        public void GetId(GridViewRow row)
        {
            Session["productCode"] = row.Cells[4].Text;
        }

        #endregion

        protected void ddlPlanCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            UCGrid1.Visible = false;
            txtPlanName.Text = "";
            pageNo = 1;

            if (ddlPlanCategory.SelectedValue != null && ddlPlanCategory.SelectedValue != "-1")
            {
                int planCat = Convert.ToInt32(ddlPlanCategory.SelectedValue);
                FillDropDownProduct(planCat);
            }

            if (ddlPlanCategory.SelectedValue == "-1")
            {
                ddlPlanCode.Enabled = false;
                txtPlanName.Enabled = false;
            }
            else
            {
                ddlPlanCode.Enabled = true;
                txtPlanName.Enabled = true;
                UCGridButtons1.Visible = false;
            }
            UCGrid1.SetText("1");
        }

        protected void ddlPlanCode_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        #region IDataPage Members - Paging


        public void Next()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                UCGrid1.SetText(pageNo.ToString());

            }
            else
            {
                pageNo = 2;
                UCGrid1.SetText("2");

            }

            Search((Convert.ToInt32(ddlPlanCategory.SelectedValue)), pageNo);
        }

        public void Previous()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                UCGrid1.SetText(pageNo.ToString());

                Search((Convert.ToInt32(ddlPlanCategory.SelectedValue)), pageNo);
            }
        }

        public void Go()
        {

            int p = int.Parse(UCGrid1.GetText());
            if (p != 0)
            {
                try
                {
                    Search((Convert.ToInt32(ddlPlanCategory.SelectedValue)), p);
                    pageNo = p;
                }
                catch (Exception)
                {

                    throw;
                }


            }

        }

        #endregion

    }
}
